package org.firecloud.nuedc2015.app;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothSocket;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import org.firecloud.bluetooth.BlueToothSelectDeviceDialog;
import org.firecloud.bluetooth.BlueToothSelectDeviceDialogCallBack;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;

public class MainActivity extends Activity implements BlueToothSelectDeviceDialogCallBack{
    final static byte BLUETHOOT_CMD_NONE=0x01;
    final static byte BLUETHOOT_CMD_SET_ANGLE=0x02;
    final static byte BLUETHOOT_CMD_SET_LENGHT=0x03;
    final static byte BLUETHOOT_CMD_FUN_12=0x04;
    final static byte BLUETHOOT_CMD_FUN_3=0x05;
    final static byte BLUETHOOT_CMD_FUN_4=0x06;
    final static byte BLUETHOOT_CMD_FUN_56=0x07;
    final static byte BLUETHOOT_CMD_STOP=0x08;
    final static float ANGLE_SPEED_DIV=0.0010652644358317056f;
    final static float ANGLE_DIV=0.00009587379922485352f;
    final static float STD_ANGLE_X=(0.0853f);
    final static float STD_ANGLE_Y=(-0.0078f);
    final static float STD_ANGLE_Z=0.0f;
    final static float STD_ANGLE_SPEED_X=(0.0128f);
    final static float STD_ANGLE_SPEED_Y=(0.03f);
    final static float STD_ANGLE_SPEED_Z=0.0f;
    private GridView gridViewStatus;
    private Button buttonLink,buttonSetLenght,buttonSetAngle,buttonStop;
    private EditText editTextLenght,editTextAngle;
    private BluetoothAdapter bluetoothAdapter;
    private BlueToothSelectDeviceDialog blueToothSelectDeviceDialog;
    private BluetoothSocket bluetoothSocket;
    private Spinner spinner;
    private ArrayAdapter<String> listStatusAdapter;
    private ArrayList<String> listStatusAdapterData;
    private int cmd =  BLUETHOOT_CMD_NONE;
    private Object thread_lock_cmd = new Object();
    private int lenght = 20;
    private int angle = 0;
    private short status_angle[] = new short[6];
    private DecimalFormat floatFamt  =   new DecimalFormat("##0.0000");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridViewStatus = (GridView)this.findViewById(R.id.main_gv_status);
        buttonLink = (Button)this.findViewById(R.id.main_btn_link);
        buttonSetAngle = (Button)this.findViewById(R.id.main_btn_set_angle);
        buttonSetLenght = (Button)this.findViewById(R.id.main_btn_set_lenght);
        editTextLenght = (EditText)this.findViewById(R.id.main_et_lenght);
        editTextAngle = (EditText)this.findViewById(R.id.main_et_angle);
        spinner = (Spinner)this.findViewById(R.id.main_spinner_question);
        buttonStop = (Button)this.findViewById(R.id.main_btn_stop);
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        bluetoothAdapter.enable();
        blueToothSelectDeviceDialog = new BlueToothSelectDeviceDialog(this,bluetoothAdapter,this);
        buttonLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                blueToothSelectDeviceDialog.show();
            }
        });
        buttonSetAngle.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View view) {
                angle = Integer.parseInt(editTextAngle.getText().toString());
                synchronized (thread_lock_cmd) {
                    cmd = BLUETHOOT_CMD_SET_ANGLE;
                }
            }
        });
        buttonStop.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View view) {
                synchronized (thread_lock_cmd) {
                    cmd = BLUETHOOT_CMD_STOP;
                }
                spinner.setSelection(4);
            }
        });
        buttonSetLenght.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View view) {
                lenght = Integer.parseInt(editTextLenght.getText().toString());
                synchronized (thread_lock_cmd) {
                    cmd = BLUETHOOT_CMD_SET_LENGHT;
                }
            }
        });
        spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this, "onItemSelected:" + i, Toast.LENGTH_SHORT).show();
                cmd = BLUETHOOT_CMD_FUN_12 + i;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        listStatusAdapterData = new ArrayList<String>();
        for(int index=0;index<6;++index){
            listStatusAdapterData.add("0.0000");
        }
        listStatusAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,listStatusAdapterData);
        gridViewStatus.setAdapter(listStatusAdapter);
    }
    @Override
    public void BlueToothSelectDeviceDialogLinkOK(BluetoothSocket bluetoothSocket) {
        this.bluetoothSocket = bluetoothSocket;
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    InputStream is= MainActivity.this.bluetoothSocket.getInputStream();
                    OutputStream os= MainActivity.this.bluetoothSocket.getOutputStream();
                    while(true){
                        synchronized (thread_lock_cmd) {
                            os.write(cmd);
                            is.read();
                            switch (cmd) {
                                case BLUETHOOT_CMD_SET_ANGLE:
                                    os.write(angle);
                                    break;
                                case BLUETHOOT_CMD_SET_LENGHT:
                                    os.write(lenght);
                                    break;
                                default:
                                    os.write(0x00);
                                    break;
                            }
                        }
                        for(int index=0;index<6;++index){
                            status_angle[index] = 0;
                            status_angle[index] |= is.read();
                            status_angle[index] |= is.read()<<8;
                        }
                        listStatusAdapterData.set(0, floatFamt.format(status_angle[0] * ANGLE_DIV + STD_ANGLE_X));
                        listStatusAdapterData.set(1, floatFamt.format(status_angle[1] * ANGLE_DIV + STD_ANGLE_Y));
                        listStatusAdapterData.set(2, floatFamt.format(status_angle[2] * ANGLE_DIV + STD_ANGLE_Z));

                        listStatusAdapterData.set(3, floatFamt.format(status_angle[3] * ANGLE_SPEED_DIV + STD_ANGLE_SPEED_X));
                        listStatusAdapterData.set(4, floatFamt.format(status_angle[4] * ANGLE_SPEED_DIV + STD_ANGLE_SPEED_Y));
                        listStatusAdapterData.set(5, floatFamt.format(status_angle[5] * ANGLE_SPEED_DIV + STD_ANGLE_SPEED_Z));
                        gridViewStatus.post(new Runnable() {
                            @Override
                            public void run() {
                                listStatusAdapter.notifyDataSetChanged();
                            }
                        });
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
    }
}
